package git.soulbgm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import git.soulbgm.common.base.BaseController;
import git.soulbgm.common.pojo.QueryDto;
import git.soulbgm.common.pojo.Response;
import git.soulbgm.common.enums.CodeEnum;
import git.soulbgm.pojo.NodeInfo;
import git.soulbgm.service.NodeInfoService;
import git.soulbgm.service.NodeStatusService;
import git.soulbgm.service.NodeUsedInfoService;
import git.soulbgm.utils.EasyPoiUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static git.soulbgm.common.MonitorConstants.*;

/**
 * 硬件设备信息控制器
 *
 * @author SoulBGM
 * @date 2022/03/11 10:28
 */
@RestController
@RequestMapping("system/node")
public class NodeController extends BaseController {

    private final NodeInfoService nodeInfoService;
    private final NodeStatusService nodeStatusService;
    private final NodeUsedInfoService nodeUsedInfoService;

    public NodeController(NodeInfoService nodeInfoService, NodeStatusService nodeStatusService, NodeUsedInfoService nodeUsedInfoService) {
        this.nodeInfoService = nodeInfoService;
        this.nodeStatusService = nodeStatusService;
        this.nodeUsedInfoService = nodeUsedInfoService;
    }

    @PostMapping("list")
    public Response list(@RequestBody QueryDto<NodeInfo> dto, Page<NodeInfo> queryPage) {
        Page<NodeInfo> resultPage = nodeInfoService.page(queryPage, new QueryWrapper<>(dto.getData()));
        return success(resultPage);
    }

    @GetMapping("export")
    public void export(NodeInfo info, HttpServletResponse response) {
        List<NodeInfo> list = nodeInfoService.list(new QueryWrapper<>(info));
        try {
            EasyPoiUtil.exportExcel(response, "主机数据", list, NodeInfo.class);
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }

    @PostMapping("add")
    public Response add(NodeInfo info) {
        if (!nodeInfoService.checkUnique(info, CHECK_UNIQUE_NODE_CODE) || !nodeInfoService.checkUnique(info, CHECK_UNIQUE_IP)) {
            return Response.setError(CodeEnum.SAVE_FAIL);
        }
        boolean result = nodeInfoService.save(info);
        if (result) {
            nodeInfoService.clearCache();
        }
        return result ? success() : error();
    }

    @PostMapping("remove")
    public Response remove(String ids) {
        boolean result = nodeInfoService.removeByIds(Arrays.asList(ids.split(",")));
        if (result) {
            nodeStatusService.removeByIds(Arrays.asList(ids.split(",")));
            nodeUsedInfoService.removeByIds(Arrays.asList(ids.split(",")));
            nodeInfoService.clearCache();
        }
        return result ? success() : error();
    }

    @PostMapping("edit")
    public Response edit(NodeInfo info) {
        if (!nodeInfoService.checkUnique(info, CHECK_UNIQUE_NODE_CODE) || !nodeInfoService.checkUnique(info, CHECK_UNIQUE_IP)) {
            return Response.setError(CodeEnum.UPDATE_FAIL);
        }
        boolean result = nodeInfoService.updateById(info);
        if (result) {
            nodeInfoService.clearCache();
        }
        return result ? success() : error();
    }

    @PostMapping({"changeStatus", "changeSelfStatus", "updateNodeLoginCredentials"})
    public Response updateNodeLoginCredentials(NodeInfo info) {
        boolean result = nodeInfoService.updateById(info);
        if (result) {
            nodeInfoService.clearCache();
        }
        return result ? success() : error();
    }

    @PostMapping("checkNodeCodeUnique")
    public String checkNodeCodeUnique(NodeInfo info) {
        return nodeInfoService.checkUnique(info, CHECK_UNIQUE_NODE_CODE) ? UNIQUE : NOT_UNIQUE;
    }

    @PostMapping("checkIpUnique")
    public String checkIpUnique(NodeInfo info) {
        return nodeInfoService.checkUnique(info, CHECK_UNIQUE_IP) ? UNIQUE : NOT_UNIQUE;
    }
}
